package com.flamingo.xiaomiweather.ui.place;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.RecyclerView;

import com.flamingo.xiaomiweather.R;
import com.flamingo.xiaomiweather.common.Result;
import com.flamingo.xiaomiweather.logic.model.PlaceResponse;


public class PlaceAdapter extends RecyclerView.Adapter<PlaceAdapter.ViewHolder>{

    private PlaceResponse placeResponse;
    private Fragment fragment;

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.place_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PlaceResponse data = placeResponse;
        if(data != null){
            String timezone = data.getTimezone();
            Float apparent = data.getResult().getRealtime().getTemperature();
            if(timezone != null){
                holder.placeCity.setText(timezone);
            }
            if(apparent != null){
                holder.placeApparent.setText(apparent.toString());
            }
        }
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public PlaceAdapter(Fragment fragment, PlaceResponse response){
        placeResponse = response;
        this.fragment = fragment;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView placeCity;
        public TextView placeApparent;

        public ViewHolder(View view) {
            super(view);
            placeCity = view.findViewById(R.id.placeCity);
            placeApparent = view.findViewById(R.id.placeApparent);
        }
    }
}
